package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.VideoDetail;
import com.serkol.ams.entity.VideoLikes;
import com.serkol.ams.entity.VideoMaster;
import com.serkol.ams.mapper.VideoMasterMapper;
import com.serkol.ams.service.VideoDetailService;
import com.serkol.ams.service.VideoLikesService;
import com.serkol.ams.service.VideoMasterService;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【video_master】的数据库操作Service实现
 * @createDate 2023-10-31 19:38:54
 */
@Service
public class VideoMasterServiceImpl extends ServiceImpl<VideoMasterMapper, VideoMaster>
        implements VideoMasterService {
    @Autowired
    private VideoDetailService detailService;
    @Autowired
    private VideoLikesService likesService;
    @Override
    public MyPageOld findByList(int pageSize, int page1) {
        Map<String,Object> felild=new HashMap<>();
        felild.put("ID","CATEGORY_ID");
        MyLambdaQueryWrapper<VideoMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(VideoMaster::getId, VideoMaster::getRate,VideoMaster::getName, VideoMaster::getContent, VideoMaster::getContent);
        queryWrapper.select(VideoMaster::getContentUg).as("contentUg")
                .select(VideoMaster::getNameUg).as("nameUg")
                .select(VideoMaster::getCategoryId).as("categoryId")
                .select(VideoMaster::getViewCount).as("viewCount")
                .select(VideoMaster::getCreateDate).as("createDate")
                .picture(VideoMaster::getImageId).as("imageUrl")
                .otherTableColmuns("video_category","NAME",felild).as("categoryName")
                .select(VideoMaster::getLikeCount).as("likeCount");


        queryWrapper.orderByDesc(VideoMaster::getId);
        Page<Map<String, Object>> page = new Page<>(page1, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPageOld.parsePage(mapPage);
    }

    @Override
    public List<Map<String, Object>> findByNewList() {
        MyLambdaQueryWrapper<VideoMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(VideoMaster::getId, VideoMaster::getRate)
                .langName(VideoMaster::getName,VideoMaster::getNameUg).as("name")
                .select(VideoMaster::getViewCount).as("viewCount")
                .picture(VideoMaster::getImageId).as("imageUrl")
                .select(VideoMaster::getLikeCount).as("likeCount");
        queryWrapper.orderByDesc(VideoMaster::getId,VideoMaster::getRate).last("LIMIT 8");

        return this.listMaps(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> findByHomeList() {
        MyLambdaQueryWrapper<VideoMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(VideoMaster::getId, VideoMaster::getRate)
                .langName(VideoMaster::getName,VideoMaster::getNameUg).as("name")
                .select(VideoMaster::getViewCount).as("viewCount")
                .picture(VideoMaster::getImageId).as("imageUrl")
                .select(VideoMaster::getLikeCount).as("likeCount");
        queryWrapper.orderByDesc(VideoMaster::getRate).last("LIMIT 4");

        return this.listMaps(queryWrapper);
    }

    @Override
    public MyPageOld findByHomePageList(int pageSize, int page1) {
        MyLambdaQueryWrapper<VideoMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(VideoMaster::getId, VideoMaster::getRate)
                .langName(VideoMaster::getName,VideoMaster::getNameUg).as("name")
                .select(VideoMaster::getViewCount).as("viewCount")
                .picture(VideoMaster::getImageId).as("imageUrl")
                .select(VideoMaster::getLikeCount).as("likeCount");
        queryWrapper.orderByDesc(VideoMaster::getRate);

        Page<Map<String, Object>> page = new Page<>(page1, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPageOld.parsePage(mapPage);
    }

    @Override
    public Map<String, Object> getInfo(long id) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        MyLambdaQueryWrapper<VideoMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(VideoMaster::getId, VideoMaster::getRate)
                .langName(VideoMaster::getName,VideoMaster::getNameUg).as("name")
                .langName(VideoMaster::getContent,VideoMaster::getContentUg).as("content")
                .select(VideoMaster::getViewCount).as("viewCount")
                .picture(VideoMaster::getImageId).as("imageUrl")
                .select(VideoMaster::getLikeCount).as("likeCount");
        queryWrapper.eq(VideoMaster::getId,id);
         Map<String, Object> map = this.getMap(queryWrapper);
         if(map!=null){
        List<Map<String, Object>>    details= detailService.findByList(id);
             List<Long>    likes= likesService.findByLikes(id,loginLog.getUserId());
        map.put("details",details);
             map.put("likes",likes);
         }else{
             map=new HashMap<>();
         }
        return map;
    }

    @Override
    public boolean view(long detailId, long masterId) {

        LambdaUpdateWrapper<VideoMaster> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VideoMaster::getId, masterId).setSql("VIEW_COUNT = VIEW_COUNT + 1").
                setSql("RATE = (LIKE_COUNT/VIEW_COUNT)*10");
         boolean update = this.update(null, updateWrapper);
         if(update){
             LambdaUpdateWrapper<VideoDetail> updateWrapper1 = new LambdaUpdateWrapper<>();
             updateWrapper1.eq(VideoDetail::getId, detailId).setSql("VIEW_COUNT = VIEW_COUNT + 1").
             setSql("RATE = (LIKE_COUNT/VIEW_COUNT)*10");
           detailService.update(null, updateWrapper1);
         }

        return update;
    }

    @Override
    public boolean like(long detailId, long masterId) {
        LoginLog loginLog = AuthConstant.getLoginLog();
     //   MyLambdaQueryWrapper<VideoLikes> queryWrapper=new MyLambdaQueryWrapper<>();

        LambdaUpdateWrapper<VideoMaster> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VideoMaster::getId, masterId).setSql("LIKE_COUNT = LIKE_COUNT + 1").
                setSql("RATE = (LIKE_COUNT/VIEW_COUNT)*10");
        boolean update = this.update(null, updateWrapper);
        if(update){
            LambdaUpdateWrapper<VideoDetail> updateWrapper1 = new LambdaUpdateWrapper<>();
            updateWrapper1.eq(VideoDetail::getId, detailId).setSql("LIKE_COUNT = LIKE_COUNT + 1").
                    setSql("RATE = (LIKE_COUNT/VIEW_COUNT)*10");
            detailService.update(null, updateWrapper1);
            VideoLikes likes=new VideoLikes();
            likes.setUserId(loginLog.getUserId());
            likes.setMasterId(masterId);
            likes.setDetailId(detailId);
            likesService.save(likes);

        }

        return update;
    }
}




